/**  
* @Title: hjbController.java
* @Package com.hjb.service.controller
* @version V1.0  
*/ 
package com.hjb.service.controller.user;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hjb.service.annotation.LogInfo;
import com.hjb.service.annotation.LogType;
import com.hjb.service.config.properties.WxProperties;
import com.hjb.service.domain.Member;
import com.hjb.service.domain.RebateDetail;
import com.hjb.service.user.UserService;
import com.hjb.service.utils.StringUtil;

/**
* @ClassName: hjbController
* @Description: TODO(用户中心)
* @author <a href="mailto:125671254@qq.com">Pan Xinwen</a>
* @date 2017年4月28日 下午4:02:26
*/
@RestController
@RequestMapping("/user/v1")
public class UserController  implements Serializable {
	
	
	private static final long	serialVersionUID	= -4728031294504766861L;
	
	private Logger log = LoggerFactory.getLogger(UserController.class);
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private WxProperties wxProperties;
	
	@RequestMapping("/wxus")
	public void wxus(HttpServletRequest request){
		
	}
	
	@LogInfo(logType=LogType.查询,operationContent="校验用户是否登录")
	@RequestMapping("/c_user")
	@ResponseBody
	public Map<String,Object> c_user(HttpServletRequest request) throws UnsupportedEncodingException{
		log.info("-------------开始获取session---------");
		Object userId = request.getSession().getAttribute("userId");
		log.info("-------------获取session结束---------");
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("code", "0");
		String rurl = request.getParameter("url").replace("denghao", "=");
		
		if(StringUtil.isEmpty(userId)){
			request.getSession().setAttribute("curl", rurl);
			String baseUrl = wxProperties.getSnsapi_base();
			result.put("code", "1");
			String url = URLEncoder.encode(wxProperties.getBase_url()+"/rest/wx/v1/redirect", "UTF-8");
			baseUrl=baseUrl+"&redirect_uri="+url+"&response_type=code&scope=snsapi_userinfo&state=2062152#wechat_redirect";
			result.put("url", baseUrl);
			return result;//没有授权登录
		}
		
		return result;
		
	}
	
	@LogInfo(logType=LogType.查询,operationContent="查询用户信息")
	@RequestMapping("/getUser")
	@ResponseBody
	public Member getUserInfo(HttpServletRequest request){
		Object userId = request.getSession().getAttribute("userId");
		log.info("-------------开始查询用户信息---------");
		//查询用户信息
		Member member = userService.getUserInfo(userId+"");
//		Member member = userService.getUserInfo("2017051322170858073337");
		log.info("-------------查询用户信息成功---------");
		String json = userService.getUserFromWx(userId+"");
		JSONObject obj = new JSONObject(json);
		member.setNickname(obj.getString("nickname"));
		member.setPhotoUrl(obj.getString("headimgurl"));
		return member;
	}
	
	@LogInfo(logType=LogType.查询,operationContent="从微信获取用户信息")
	@RequestMapping("/getUfwx")
	@ResponseBody
	public String getUserFromWx(HttpServletRequest request){
		return userService.getUserFromWx(request.getSession().getAttribute("userId")+"");
	}
	
	@LogInfo(logType=LogType.修改,operationContent="补全个人信息")
	@RequestMapping("/supplement")
	@ResponseBody
	public int supplement(HttpServletRequest request){
		return userService.supplement(request);
	}
	
	@LogInfo(logType=LogType.修改,operationContent="查询用户返利信息")
	@RequestMapping("/interest")
	@ResponseBody
	public List<Map<String,Object>> getInterest(HttpServletRequest request){
		return userService.getInterest(request.getSession().getAttribute("userId")+"");
	}
	
	
	@LogInfo(logType=LogType.修改,operationContent="查询用户返利总金额")
	@RequestMapping("/interestc")
	@ResponseBody
	public String getInterestCount(HttpServletRequest request){
		return userService.getInterestCount(request.getSession().getAttribute("userId")+"");
	}
}
